<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Company extends CI_Controller {

	/**
	 * Index Page for this controller.
	 *
	 * Maps to the following URL
	 * 		http://example.com/index.php/welcome
	 *	- or -  
	 * 		http://example.com/index.php/welcome/index
	 *	- or -
	 * Since this controller is set as the default controller in 
	 * config/routes.php, it's displayed at http://example.com/
	 *
	 * So any other public methods not prefixed with an underscore will
	 * map to /index.php/welcome/<method_name>
	 * @see http://codeigniter.com/user_guide/general/urls.html
	 */
	public function index($name_roman)
	{
		// helper
		$this->load->helper('url');
		
		$this->load->library('user_agent');

		if ($this->agent->is_robot())
		{
		    $agent = $this->agent->robot();
		}
		elseif ($this->agent->is_mobile("iphone") || $this->agent->is_mobile())
		{
		    $agent = $this->agent->mobile();
			
			redirect("/mobile/$name_roman", "refresh");
		}
		else
		{
		    $agent = 'Unidentified User Agent';
		}
		
		//show_error($agent);
		
		$this->load->model('Company_model', 'company');
		$id = $this->company->get_id_from_name($name_roman);

		
		$company = $this->company->get_company_info($id);
		$company_meta = $this->company->get_company_meta_info($id);
		
		//show_error(print_r($company));
		
		// images string into array
		$company->images = explode(",", $company->images);
		
		// image comment
		$company->img_cmt = explode(",", $company->img_cmt);
		
		
		$this->load->view('pc/corp', array(
			"company" => $company,
			"company_meta" => $company_meta
		));
	}
	
	public function list_type($type){
		
		$this->load->model("Intern_type_model", "intern_type");
		$typeid = $this->intern_type->type2id($type);
		
		$this->load->library('myclass');
		$companies = $this->myclass->get_list_type($typeid);
		
		//if ($type === "programmer"){
		$type_image1 = "http://www.intern55.com/syokusyu_img/".$type.".jpg";
		$type_image2 = "http://www.intern55.com/syokusyu_img/catch_".$type.".jpg";
		//}
		
		$this->load->view('pc/intern_type/programmer', array(
			"companies" => $companies,
			"image_url1" => $type_image1,
			"image_url2" => $type_image2
		));
	}
	
	public function list_area($area){
		$this->load->model("Area_model", "area");
		$areaid = $this->area->area2id($area);
		
		$this->load->library('myclass');
		$companies = $this->myclass->get_list_area($areaid);
		
		$image_url2 = "http://www.intern55.com/place_img/".$area.".jpg";
		
		$this->load->view('pc/intern_type/programmer', array(
			"companies" => $companies,
			//"type_image1" => $type_image1,
			"image_url2" => $image_url2
		));
	}
	
	public function list_what($whatid){
		$this->load->library('myclass');
		$companies = $this->myclass->get_list_what($whatid);
		
		$image_url2 = "http://www.intern55.com/what/what". $whatid .".jpg";
		
		$this->load->view('pc/intern_type/what', array(
			"companies" => $companies,
			//"type_image1" => $type_image1,
			"image_url2" => $image_url2
		));
	}
	
	public function list_all(){
		$this->load->library('myclass');
		$companies = $this->myclass->get_list_all();
		
		$image_url2 = "/images/kigyouichiran.jpg";
		
		$this->load->view('pc/intern_type/programmer', array(
			"companies" => $companies,
			//"type_image1" => $type_image1,
			"image_url2" => $image_url2
		));
	}
	
	public static $knownreasons= array(
			"検索エンジン(Yahoo, Google等)",
			"intern55のTwitter",
			"intern55のFacebook",
			"配られたビラ",
			"置いてあるor貼ってあるチラシ",
			"店頭のポスター",
			"知人・先輩のロコミ(Twitter, Facebook等も含む)",
			"その他"
		);
	
	public function form($name_roman){
		$this->load->model('Company_model', 'company');
		$id = $this->company->get_id_from_name($name_roman);
		//show_error($id);
		
		$company = $this->company->get_company_info($id);
		
		$this->load->helper(array('form', 'url'));
		
		$this->load->view('pc/form', array(
			"company" => $company
		));
		
		
	}
	
	public function receive_form($id){
		// get company's all info
		$this->load->model('Company_model', 'company');		
		$company = $this->company->get_company_info($id);
		
		$this->load->helper(array('form', 'url'));

		$this->load->library('form_validation');
		$this->form_validation->set_error_delimiters('<div class="error">', '</div>');
		
		$this->form_validation->set_rules('name', 'お名前', 'required');
		$this->form_validation->set_rules('furigana', 'フリガナ', 'required');
		$this->form_validation->set_rules('mail', 'メール', 'required|valid_email');
		$this->form_validation->set_rules('school', '学校名', 'required');
		$this->form_validation->set_rules('gakubu', '学部名', '');
		$this->form_validation->set_rules('gakunen', '学年', 'required');
		$this->form_validation->set_rules('eki', '最寄り駅', '');
		$this->form_validation->set_rules('reason_1', 'しよう理由', '');
		$this->form_validation->set_rules('reasons', '志望理由', '');
		$this->form_validation->set_rules('twitter', 'twitterアカウント', '');
		$this->form_validation->set_rules('knownreasons', '知ったきっかけ', 'required');
		$this->form_validation->set_rules('selfPR', '自己PR', '');
		$this->form_validation->set_rules('phone', '電話番号', '');

		// $info push to check page
		$info["お名前"] = $_POST["name"];
		
		$info["フリガナ"] = $_POST["furigana"];
		
		$info["メールアドレス(PC)"] = $_POST["mail"];
		
		$info["電話番号"] = $_POST["phone"];
		
		$info["学校名"] = $_POST["school"];
		
		$info["学部名"] = $_POST["gakubu"];
		
		$info["学年"] = $_POST["gakunen"];
		
		$info["最寄り駅"] = $_POST["eki"];
		
		$info["twitterアカウント"] = $_POST["twitter"];
		
		$info["インターンをしよう理由"] = $_POST["reason_1"];
		
		$info["志望理由"] = $_POST["reasons"];
		
		$info["自己PR"] = $_POST["selfPR"];
		
		$info["知ったきっかけ"] = $_POST["knownreasons"];
		
		
		if ($this->form_validation->run() == FALSE)
		{
			$this->load->view('pc/form', array(
				"company" => $company
			));
		}
		else
		{
			//$this->load->view('formsuccess');
			
			$info["知ったきっかけ"] = Company::$knownreasons[$info["知ったきっかけ"]];
			
			$this->load->view('pc/form_kakunin', array(
				"info" => $info,
				"post" => $_POST,
				"company" => $company
			));
			
			//echo $this->email->print_debugger();
		}
	}

	public function send_form($id){
		$this->load->model('Company_model', 'company');		
		$company = $this->company->get_company_info($id);
		
		// $info push to check page
		$info["お名前"] = $_POST["name"];
		
		$info["フリガナ"] = $_POST["furigana"];
		
		$info["メールアドレス(PC)"] = $_POST["mail"];
		
		$info["電話番号"] = $_POST["phone"];
		
		$info["学校名"] = $_POST["school"];
		
		$info["学部名"] = $_POST["gakubu"];
		
		$info["学年"] = $_POST["gakunen"];
		
		$info["最寄り駅"] = $_POST["eki"];
		
		$info["twitterアカウント"] = $_POST["twitter"];
		
		$info["インターンしようと思った理由"] = $_POST["reason_1"];
		
		$info["志望理由"] = $_POST["reasons"];
		
		$info["自己PR"] = $_POST["selfPR"];
		
		$info["知ったきっかけ"] = Company::$knownreasons[$_POST["knownreasons"]];
		
		$mailcontent = "";
		foreach ($info as $key => $value) {
			$mailcontent .= "$key: $value\n";
		}
		//echo nl2br($mailcontent);
		
		//log_message("info", $mailcontent);
		$this->log_apply($info);
		
		$this->load->library('email');
		
		$this->email->from('your@example.com', 'Intern55');
		//$this->email->to('dttung1412@gmail.com');
		$this->email->to('nishimura@berryinc.jp'); 
		$this->email->cc('nagashima@berryinc.jp');
		$this->email->bcc('them@their-example.com');
		
		$this->email->subject( "インターンシップお問い合わせ　".$company->kaisha_mei );
		$this->email->message($mailcontent);
		
		//echo $this->email->print_debugger();
		if ( $this->email->send() )
			$this->load->view('pc/thanks', array(
			));
	}

	function log_apply($info){
		
		$content = '';
		
		$filename =  $_SERVER['DOCUMENT_ROOT'] . '/../log_apply_intern55.cgi';
		
		if (!is_writable($filename)){
			foreach ( array_keys($info) as $key) {
				$content .= $key . ',';
			}
			$content .= "\r\n";
		}
		
		foreach ( $info as $key => $value){
			$content .= $value . ',';
		}
		$content .= "\r\n";
		
		
		//$somecontent = date("Y-m-d H:i:s")."\n$content\n";
		$somecontent = $content;
		
		
			// In our example we're opening $filename in append mode.
		    // The file pointer is at the bottom of the file hence
		    // that's where $somecontent will go when we fwrite() it.
		    if (!$handle = fopen($filename, 'a')) {
		         echo "Cannot open file ($filename)";
		         exit;
		    }
		
		    // Write $somecontent to our opened file.
		    if (fwrite($handle, $somecontent) === FALSE) {
		        echo "Cannot write to file ($filename)";
		        exit;
		    }
		
		    //echo "Success, wrote ($somecontent) to file ($filename)";
		
		    fclose($handle);
	}

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */